home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / elk-2_0.lha / elk-2.0 / src / stab.bsd.c < prev    next >
C/C++ Source or Header  |  1992-10-04  |  2KB  |  68 lines

  1. #include <a.out.h>
  2. #include <sys/types.h>
  3.  
  4. SYMTAB *Snarf_Symbols (f, ep) FILE *f; struct exec *ep; {
  5.     SYMTAB *tab;
  6.     register SYM *sp, **nextp;
  7.     int nsyms, strsiz;
  8.     struct nlist nl;
  9.  
  10.     tab = (SYMTAB *)Safe_Malloc (sizeof (SYMTAB));
  11.     tab->first = 0;
  12.     tab->strings = 0;
  13.     nextp = &tab->first;
  14.     (void)fseek (f, (long)N_SYMOFF(*ep), 0);
  15.     for (nsyms = ep->a_syms / sizeof (nl); nsyms > 0; nsyms--) {
  16.     if (fread ((char *)&nl, sizeof (nl), 1, f) != 1) {
  17.         Free_Symbols (tab);
  18.         (void)fclose (f);
  19.         Primitive_Error ("corrupt symbol table in object file");
  20.     }
  21.     if (nl.n_un.n_strx == 0 || nl.n_type & N_STAB)
  22.         continue;
  23. #ifndef ibm023
  24.     if ((nl.n_type & N_TYPE) != N_TEXT)
  25.         continue;
  26. #endif
  27.     sp = (SYM *)Safe_Malloc (sizeof (SYM));
  28.     sp->name = (char *)nl.n_un.n_strx;
  29.     sp->value = nl.n_value;
  30.     *nextp = sp;
  31.     nextp = &sp->next;
  32.     *nextp = 0;
  33.     }
  34.     if (fread ((char *)&strsiz, sizeof (strsiz), 1, f) != 1) {
  35. strerr:
  36.     Free_Symbols (tab);
  37.     (void)fclose (f);
  38.     Primitive_Error ("corrupt string table in object file");
  39.     }
  40.     if (strsiz <= 4)
  41.     goto strerr;
  42.     tab->strings = Safe_Malloc (strsiz);
  43.     strsiz -= 4;
  44.     if (fread (tab->strings+4, 1, strsiz, f) != strsiz)
  45.     goto strerr;
  46.     for (sp = tab->first; sp; sp = sp->next)
  47.     sp->name = tab->strings + (long)sp->name;
  48.     return tab;
  49. }
  50.  
  51. #ifdef INIT_OBJECTS
  52. SYMTAB *Open_File_And_Snarf_Symbols (name) char *name; {
  53.     struct exec hdr;
  54.     FILE *f;
  55.     SYMTAB *tab;
  56.  
  57.     if ((f = fopen (name, "r")) == NULL)
  58.     Primitive_Error ("can't open a.out file");
  59.     if (fread ((char *)&hdr, sizeof hdr, 1, f) != 1) {
  60.     (void)fclose (f);
  61.     Primitive_Error ("can't read a.out header");
  62.     }
  63.     tab = Snarf_Symbols (f, &hdr);
  64.     (void)fclose (f);
  65.     return tab;
  66. }
  67. #endif /* INIT_OBJECTS */
  68.